home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-03-04 | 6.3 KB | 269 lines | [TEXT/ToyS] |
- -- Properties
- property kasPrefName : "Search & Replace V1.0"
- property kasDoOnlyText : true
-
- -- Globals
- global gasInfoWind -- Info window
- global gasInfoPos -- Position of info window
-
- global gasFoldersToDo -- The folders left to process
- global gasChecked -- Number checked!
- global gasSaved -- Number changed
-
- global gasSearch -- Search string
- global gasReplace -- Replace with
- global gasDoRsrc, gasDoData
-
-
- on open fsObjs
- -- Load prefs, show window
- pfLoad()
-
- -- Set up prefix
- set gasChecked to 0
- set gasSaved to 0
-
- GetInputs()
-
- set gasInfoWind to display info titled kasPrefName ¬
- located at gasInfoPos ¬
- message "Scanning…"
-
- display info gasInfoWind ¬
- message ("Search: " & gasSearch) ¬
- at line 8
-
- display info gasInfoWind ¬
- message ("Replace: " & gasReplace) ¬
- at line 9
-
- -- Do files
- set gasFoldersToDo to {}
-
- repeat with fsObj in fsObjs
- set myInfo to (basic info for fsObj)
-
- if (system type of myInfo is "fold") then
- set gasFoldersToDo to gasFoldersToDo & {fsObj}
- else
- DoOne(fsObj)
- end if
- end repeat
-
- -- Do folders
- repeat while gasFoldersToDo is not {}
- -- Pop one off the end
- set n to the number of items of gasFoldersToDo
- set fsObj to item n of gasFoldersToDo
-
- if (n > 1) then
- set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
- else
- set gasFoldersToDo to {}
- end if
-
- display info gasInfoWind ¬
- message ("Folders to go: " & n) ¬
- at line 6 ¬
- using color (15 * 32)
-
- -- Process it
- GoDeep(fsObj)
- end repeat
-
- display info gasInfoWind message "DONE!"
-
- pause for 3 with seconds timing -- Let screen wait...
-
- set gasInfoPos to screen location of ¬
- (display info gasInfoWind with disposal)
-
- pfSave() -- Save window location
- end open
-
-
- on GetInputs()
- set gasSearch to GetString("search string", gasSearch)
- set gasReplace to GetString("replace string", gasReplace)
-
- if (length of gasReplace) is not (length of gasSearch) then ¬
- if (ShowChoice("CAUTION:" & return & return & "Search & replace are not the same length", ¬
- {"Cancel", "I know what I'm doing!", "Padded Pascal"}) is "Padded Pascal") then
- -- Make pascal string
- set n to length of gasReplace
- set gasReplace to (ASCII character n) & gasReplace
- set n to n + 1
-
- -- Make pascal string
- set m to length of gasSearch
- set gasSearch to (ASCII character m) & gasSearch
- set m to m + 1
-
- -- Pad them to same length
- if (n > m) then set m to n
- repeat while (length of gasReplace) < m
- set gasReplace to gasReplace & (ASCII character 0)
- end repeat
- repeat while (length of gasSearch) < m
- set gasSearch to gasSearch & (ASCII character 0)
- end repeat
- end if
-
- set choice to ShowChoice("Replace in Data, Resource or Both forks?", ¬
- {"Data", "Rsrc", "Both"})
-
- set gasDoData to (choice is "Data") or (choice is "Both")
- set gasDoRsrc to (choice is "Rsrc") or (choice is "Both")
- end GetInputs
-
-
- on GetString(sName, sDef)
- return text returned of ¬
- (display dialog ("Enter the " & sName & ":") ¬
- buttons {"Cancel", "OK"} ¬
- default answer sDef ¬
- default button 2 ¬
- with icon note)
- end GetString
-
-
- on ShowAlert(msg)
- return button returned of ¬
- (display dialog msg ¬
- buttons {"Cancel", "Yo!"} ¬
- default button 2 ¬
- with icon stop)
- end ShowAlert
-
-
- on ShowChoice(msg, choices)
- return button returned of ¬
- (display dialog msg ¬
- buttons choices ¬
- default button (number of items of choices) ¬
- with icon caution)
- end ShowChoice
-
-
- on DoOne(fsObj)
- set fInfo to (basic info for fsObj)
- set fname to (catalog name of fInfo)
-
- set gasChecked to gasChecked + 1
-
- display info gasInfoWind ¬
- message fname ¬
- at line 2
-
- if (gasDoData and (data fork length of fInfo > 0)) then
- set fileData to read data from the data fork of fsObj
-
- display info gasInfoWind ¬
- message ("Checking Data: " & (data fork length of fInfo)) ¬
- at line 3
-
- set newData to munge fileData ¬
- searching for gasSearch ¬
- replacing it with gasReplace
-
- if (not (the same data is in fileData as in newData)) then
- set gasSaved to gasSaved + 0.5
- write data to the data fork of fsObj ¬
- from buffer newData
- display info gasInfoWind ¬
- message ("Saved Data") ¬
- at line 3
- end if
- end if
-
- if (gasDoRsrc and (resource fork length of fInfo > 0)) then
- display info gasInfoWind ¬
- message ("Checking Resource: " & (resource fork length of fInfo)) ¬
- at line 4
- set fileRef to ¬
- open fork from fsObj ¬
- with resource and write access
-
- set fileData to read data from fileRef
-
- set newData to munge fileData ¬
- searching for gasSearch ¬
- replacing it with gasReplace
-
- if (not (the same data is in fileData as in newData)) then
- if (the data length of newData) is not (the data length of fileData) then ¬
- ShowAlert("Please cancel, you will corrupt this file!")
- set gasSaved to gasSaved + 0.5
- size fork fileRef given «class len »:0
- write data to fileRef ¬
- from buffer newData
-
- display info gasInfoWind ¬
- message ("Saved Resources") ¬
- at line 4
- end if
-
- close fork fileRef
- end if
- end DoOne
-
-
- on GoDeep(foldObj)
- display info gasInfoWind ¬
- message "Path: " & (foldObj as string)
-
- set daddy to foldObj as string
-
- -- Do kinds that match
- display info gasInfoWind ¬
- message "Scanning files" at line 5
-
- if (kasDoOnlyText) then
- set myItems to the entries in foldObj ¬
- whose kinds are a file ¬
- whose types are in {"TEXT"}
- else
- set myItems to the entries in foldObj ¬
- whose kinds are a file
- end if
-
- repeat with myItem in myItems
- DoOne((daddy & myItem) as alias)
- end repeat
-
- -- Do folders
- display info gasInfoWind ¬
- message "Scanning subfolders" at line 5
-
- set myItems to the entries in foldObj ¬
- whose kinds are a folder
-
- repeat with myItem in myItems
- set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
- end repeat
-
- -- Done
- display info gasInfoWind ¬
- message "…" at line 5
- end GoDeep
-
-
- on pfLoad()
- try
- set ourPrefs to (load preference named kasPrefName)
- on error
- set ourPrefs to {{8, 48}, ">LORE<", "◊LIFE◊", true, true}
- end try
-
- set gasInfoPos to item 1 of ourPrefs
- set gasSearch to item 2 of ourPrefs
- set gasReplace to item 3 of ourPrefs
- set gasDoData to item 4 of ourPrefs
- set gasDoRsrc to item 5 of ourPrefs
- end pfLoad
-
-
- on pfSave()
- save preference {gasInfoPos, gasSearch, gasReplace, gasDoData, gasDoRsrc} named kasPrefName
- end pfSave
-